package com.tansun.easycare.rule.ml.model.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.StringUtils;
import com.tansun.easycare.core.web.BaseController;
import com.tansun.easycare.rule.ml.algorithm.entity.MlDataModelVO;
import com.tansun.easycare.rule.ml.algorithm.entity.MlWorkflows;
import com.tansun.easycare.rule.ml.algorithm.entity.PyResponse;
import com.tansun.easycare.rule.ml.algorithm.service.IAlgorthmService;
import com.tansun.easycare.rule.ml.model.entity.MlDataModel;
import com.tansun.easycare.rule.ml.model.service.MlDataModelService;

/**
 * 模型的创建，修改，删除，查找及应用Controller
 * @author zkx
 * @version 2018-08-22
 */
@Controller
@RequestMapping(value = "/ml/mlDataModel")
public class MlDataModelController extends BaseController {

	@Autowired
	private MlDataModelService mlDataModelService;

	@Autowired
	private IAlgorthmService iAlgrthmService;
	
	@ModelAttribute
	public MlDataModel get(@RequestParam(required=false) String id) {
		MlDataModel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mlDataModelService.get(id);
		}
		if (entity == null){
			entity = new MlDataModel();
		}
		return entity;
	}
	
	//查找
	@RequestMapping(value = {"list", ""})
	public String list(MlDataModel mlDataModel, HttpServletRequest request, HttpServletResponse response, Model model) {
		mlDataModel.getPage().setOrderBy("model_id");
		PageInfo<MlDataModel> page = mlDataModelService.findPage(mlDataModel.getPage(), mlDataModel);
		model.addAttribute("page", page);
		return "rule/ml/mlDataModelList";
	}

	//创建
	@RequestMapping(value = "create")
	public String create(Model model) {
		MlDataModel mlDataModel = new MlDataModel();
		//mlDataModel.setUserId();
		mlDataModel.setCreateTime(new Date());;
		model.addAttribute("mlDataModel", mlDataModel);
		return "rule/ml/mlDataModelForm";
	}
	
	//保存
	@RequestMapping(value = "save")
	public String save(MlDataModel mlDataModel, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		 String flag=request.getParameter("flag");
		 if(StringUtils.isEmpty(flag) || "0".equals(flag)){
			//保存进数据库
			mlDataModelService.save(mlDataModel);
			int modelId = mlDataModelService.getIdentify();
			mlDataModel.setModelId(modelId);
			//在服务中创建模型
			try{
				iAlgrthmService.createModel(mlDataModel);
			}catch(RuntimeException e){
				mlDataModelService.delete(mlDataModel);
			}
		}else{
			iAlgrthmService.updateModel(mlDataModel);
			mlDataModelService.update(mlDataModel);
		}

		addMessage(redirectAttributes, "保存模型成功");
		return "redirect:"+"/ml/mlDataModel/?repage";
	}

	//删除
	@RequestMapping(value = "delete")
	public String delete(MlDataModel mlDataModel, RedirectAttributes redirectAttributes) {
		iAlgrthmService.delModel(mlDataModel);
		mlDataModelService.delete(mlDataModel);
		addMessage(redirectAttributes, "删除模型成功");
		return "redirect:"+"/ml/mlDataModel/?repage";
	}

	//删除
	@RequestMapping(value = "deleteList")
	public String delete(MlDataModelVO mlDataModelVO, RedirectAttributes redirectAttributes) {
		List<MlDataModel> mlDataModelList = mlDataModelVO.getMlDataModelList();
		iAlgrthmService.delModel(mlDataModelList);
		for(MlDataModel mlDataModel:mlDataModelList){
			mlDataModelService.delete(mlDataModel);
		}
		addMessage(redirectAttributes, "删除模型成功");
		return "redirect:"+"/ml/mlDataModel/?repage";
	}
	
	//更新
	@RequestMapping(value = "Listupdate")
	public String Listupdate(MlDataModel mlDataModel, Model model){
		MlDataModel mlModel = mlDataModelService.findByModelId(mlDataModel);
		model.addAttribute("mlDataModel", mlModel);
		return "rule/ml/mlDataModelForm";
	}
	
	//应用，打开流程图
	@RequestMapping(value = "mlDataModelFlowChart")
	public String mlDataModelFlowChart(MlDataModel mlDataModel, RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.loadModel(mlDataModel);
		PyResponse wfResp = iAlgrthmService.getWorkflows(mlDataModel,null);
		MlWorkflows workflows= MlWorkflows.newInstance(wfResp.getResponse());
		model.addAttribute("workflows", workflows);
		model.addAttribute("mlDataModel", mlDataModel);
		addMessage(redirectAttributes, "打开模型成功");
		String algorithmType = mlDataModel.getAlgorithmType();
		return "rule/ml/"+algorithmType+"/mlDataModelFlowChart";
	}

	//模型保存成可以发布的形式pmml
	@RequestMapping(method = RequestMethod.GET, value = "/transSaveType")
	public @ResponseBody JSONObject transSaveType(MlDataModel mlDataModel) {
		PyResponse resp = iAlgrthmService.transSaveType(mlDataModel);
		JSONObject savepath = (JSONObject) resp.getResponse().get("transSaveType");
		mlDataModel.setModelStatus("published");
		mlDataModelService.updateModelStatus(mlDataModel);
		return savepath;
	}
	
}